<template>
  <div>
    <p class="head">文章评论:</p>
    <!-- 评论列表 -->
    <van-list
      v-model="isLoading"
      :finished="finished"
      @load="onLoad"
      finished-text="没有更多了"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <template>
          <div class="cell-box">
            <!-- 图片 -->
            <van-image round fit="cover" :src="item.aut_photo" />
            <!-- div -->
            <div class="info-box">
              <div style="color: #466bb5">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div>
                <span class="time">{{ item.pubdate | relativeTime }}</span>
                <van-button size="mini" plain
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <!-- 图标 -->
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 发表评论框 -->
    <div style="position: fixed; left: 0; bottom: 0; width: 100%">
      <van-field
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { getCommentsAPI } from "@/api";
import { sendCommentAPI } from "@/api";
export default {
  data() {
    return {
      isLoading: false,
      finished: false,
      offset: null,
      limit: 10,
      list: [],
      cmt: "",
    };
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true,
    },
    target: {
      type: [Number, String, Object],
      required: true,
    },
  },
  methods: {
    async onLoad() {
      try {
        const { data } = await getCommentsAPI({
          type: "a",
          source: this.source,
          offset: this.offset,
          limit: this.limit,
        });
        // 保存到数组
        const { results } = data.data;
        this.list.push(...results);

        // 记录last_id方便查下一页
        this.offset = res.data.data.last_id

        // 设置本次加载完毕
        this.isLoading = false;

        // 是否还有数据
        if (results.length) {
          //  有就更新下一页的数据页码
          this.offset = data.data.last_id;
        } else {
          // 没有就关掉finished
          this.finished = true;
        }
      } catch (err) {
        this.isLoading = false;
      }
    },
    // 发表评论
    async send() {
      // 非空判断
      if (this.cmt.trim() == "") return this.$toast("请输入点内容");

      try {
        // 发请求
        const { data } = await sendCommentAPI({
          target: this.target,
          content: this.cmt,
          art_id: null,
        });
        this.$toast.success("发布成功");
        console.log("评论成功", data);

        // 清空文本
        this.cmt = "";
        this.list.unshift(data.new_obj);
      } catch (err) {
        this.$toast.fail("发布失败");
        console.log("发布失败", err);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  color: #4e8de6;
  font-size: 36px;
}
.cell-box {
  display: flex;
  .van-image {
    width: 50px;
    height: 50px;
    margin-right: 20px;
  }

  .info-box {
    flex: 1;

    .time {
      color: gray;
      margin-right: 10px;
    }
    .van-button {
      // 文字对齐方式为中线对齐
      vertical-align: middle;
    }
  }
}
</style>